﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Transactions;
using Model;
using MsDAL;
using System.ComponentModel;

namespace ServiceHelper
{
    [DataObjectAttribute]
    public partial class Ord4a00Manager : Ord4a00Base
    {
        private Ord4a00DAL ord4a00Dal = new Ord4a00DAL();

        private static Ord4a00Manager _entity;

        public static Ord4a00Manager Entity
        {
            get
            {
                if (_entity == null)
                    _entity = new Ord4a00Manager();
                return _entity;
            }
        }

        #region 个人代码
        public string SaveData(Hashtable tb, DataTable dtl, DataTable deldata, string action, string usercode)
        {
            List<SqlApp> lst = new List<SqlApp>();

            Ord4a00 model = null;
            Ord4a01 dtlModel = null;
            if ("A" == action && (dtl == null || dtl.Rows.Count == 0)) return "明细中没有数据，不需要保存！";

            string appstr = Context.GetAppByUserCode(usercode);

            using (var tran = new TransactionScope())
            {
                if ("A" == action) //新增
                {
                    //保存概要表身
                    model = new Ord4a00();
                    Components.CommonUtil.GetInstance.CopyProperties(tb, model);

                    model.Audstatus = 1;
                    model.Listid = Sys0a60Manager.Entity.CreateSn("ord4a00_listid", usercode);
                    if (string.IsNullOrEmpty(model.Outerdocno))
                        model.Outerdocno = Sys0a60Manager.Entity.CreateSn("ord4a_outerno", usercode);

                    model.Muser = appstr;
                    lst.Add(this.Add(model));

                    foreach (DataRow dr in dtl.Rows)
                    {
                        dtlModel = new Ord4a01();
                        Components.CommonUtil.GetInstance.CopyProperties(dtlModel, dr);
                        dtlModel.Listid =
                            Components.Validation.ToInt(Sys0a60Manager.Entity.CreateSn("ord4a01_listid", usercode));
                        dtlModel.Plistid = model.Listid;

                        lst.Add(Ord4a01Manager.Entity.Add(dtlModel));
                    }
                }
                else if ("M" == action)
                {
                    model = this.GetModelById(Components.Validation.ToString(tb["listid"]));
                    if (null == model) return "该笔退单可能已经被删除！";
                    Components.CommonUtil.GetInstance.CopyProperties(tb, model);

                    lst.Add(this.Update(model));

                    foreach (DataRow dr in dtl.Rows)
                    {
                        dtlModel = new Ord4a01();
                        Components.CommonUtil.GetInstance.CopyProperties(dtlModel, dr);

                        if (!Components.Validation.IsNull(dr["listid"]))
                        {
                            lst.Add(Ord4a01Manager.Entity.Modify(dtlModel));
                        }
                        else
                        {
                            dtlModel.Listid =
                                Components.Validation.ToInt(Sys0a60Manager.Entity.CreateSn("ord4a01_listid", usercode));
                            dtlModel.Plistid = model.Listid;

                            lst.Add(Ord4a01Manager.Entity.Add(dtlModel));
                        }
                    }

                    if (null != deldata)
                    {
                        foreach (DataRow dr in dtl.Rows)
                        {
                            dtlModel = new Ord4a01();
                            dtlModel.Listid = Components.Validation.ToInt(dr["listid"]);
                            lst.Add(Ord4a01Manager.Entity.Delete(dtlModel));
                        }
                    }
                }
                else if ("P" == action) //审核
                {
                    lst.Add(ord4a00Dal.Audit(Components.Validation.ToString(tb["listid"])));
                }
                else if ("S" == action) //收货入库
                {
                    foreach (DataRow dr in dtl.Rows)
                    {
                        lst.Add(ord4a00Dal.Back(Components.Validation.ToString(dr["listid"]),
                                                Components.Validation.ToDouble(dr["accqty"]),
                                                Components.Validation.ToDouble(dr["accamt"])));
                    }

                    //收货
                    lst.Add(ord4a00Dal.Receiving(Components.Validation.ToString(tb["listid"])));
                }

                var ret = CommonManager.Entity.SaveData(lst);

                if (string.IsNullOrEmpty(ret))
                    tran.Complete();

                return ret;
            }
        }

        #endregion

        public SqlApp DoAdjust(string rowId, double qty, double amt)
        {
            return ord4a00Dal.DoAdjust(rowId, qty, amt);
        }
    }
}



